<?php

abstract class View extends Renderable
{
	public
		$title = "",
			  
		$css_dir = "css/",
		$js_dir = "js/",
	
		$meta = array(
			"name" => array(
				"author" => "",
				"classification" => "",
				"copyright" => "",
				"keywords" => "",
				"description" => "",
			),
		 
			"http-equiv" => array(
				"Content-Type" => "text/html; charset=utf-8",
				"Content-language" => "ru",
				"Cache-Control" => "",
			),
		);
	
	protected
		$template = null;
	
	public function Render($arg)
	{
		ln('<!DOCTYPE html>');
		ln('<html>');
		
			ln('<head>');
			
				$this->RenderMeta();
				$this->RenderFavIcon();
				$this->RenderCachedCss($this->GetStyles());
				$this->RenderJsBody();
				ln('<title>'.$this->title.'</title>');
				
			ln('</head>');
			
			ln('<body>');
	
				$this->RenderHeader();
			
				ln('<div id="page">');
				
				$this->RenderBody($arg);
					
				ln('</div>');
				
				$this->RenderFooter();
			
				ln('<div id="scripts">');
					$this->RenderCachedJs($this->GetJS());
				ln('</div>');
			
			ln('</body>');
			
		ln('</html>');
	}
	
	protected function RenderHeader()
	{
		ln('<div id="header">');
		ln('</div>');
	}
	
	protected function RenderFooter()
	{
		ln('<div id="footer">');
		ln('</div>');
	}
	
	private function RenderMeta()
	{
		foreach ($this->meta as $type)
		{
			foreach ($type as $key => $value)
			{
				if ($value)
				{
					ln("<meta {$type}=\"{$key}\" content=\"{$value}\"/>");
				}
			}
		}
	}
	
	public function RenderBody($arg)
	{
		/* использование шаблонов с помощью Twig */
		$loader = new Twig_Loader_Filesystem( TEMPLATES_PATH );
		$twig = new Twig_Environment($loader, array( 'cache' => TWIG_CACHE ));

		$this->template = $twig->loadTemplate(get_class($this).'.html');
		
		$this->RenderTemplates($arg);
		
		if (DEV_SERVER)
		{
			$twig->clearCacheFiles();
		}
	}
	
	protected function RenderTemplates() {}

	protected function GetStyles()
	{
		return array("global");
	}
	
	protected function GetJS()
	{
		return array("jquery-1.6.min");
	}
	
	private function RenderCssFile($filename)
	{
		echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.".HOSTNAME."/".$this->css_dir.$filename.".css\" />\n";
	}
	
	private function RenderJsFile($filename)
	{
		echo "<script type=\"text/javascript\" language=\"javascript\" src=\"http://www.".HOSTNAME."/".$this->js_dir.$filename.".js\"></script>\n";
	}
	
	protected function RenderCssFiles()
	{
		$args = array_shift( func_get_args() );
		
		foreach($args as $arg)
		{
			if (is_string($arg))
			{
				$this->RenderCssFile($arg);
			}
			elseif (is_array($arg))
			{
				$this->RenderCssFiles($arg);
			}
		}
	}
	
	protected function RenderCachedCss()
	{
		$args = func_get_args();
		
		if (isset($_GET['debug']))
		{
			return $this->RenderCssFiles($args);
		}

		$files = array();
		$cached_files = array();
		
		foreach($args as $arg)
		{
			if (is_string($arg))
			{
				$files[] = $arg;
			}
			elseif (is_array($arg))
			{
				foreach($arg as $item)
				{
					$files[] = $item;
				}
			}
		}
		
		foreach($files as $file)
		{
			$mtime = filemtime($this->css_dir.$file.".css");
			
			if ($mtime)
			{
				$cached = $file."-".(md5($mtime));
				if (!file_exists($this->css_dir.$cached.".css"))
				{
					file_put_contents($this->css_dir.$cached.".css", 
						minimize_str(file_get_contents($this->css_dir.$file.".css"))
					);
				}
				
				$cached_files[] = $cached;
			}
		}
		
		$this->RenderCssFiles($cached_files);
	}
	
	protected function RenderJsFiles()
	{
		$args = array_shift(func_get_args());
		
		foreach($args as $arg)
		{
			if (is_string($arg))
			{
				$this->RenderJsFile($arg);
			}
			elseif (is_array($arg))
			{
				$this->RenderJsFiles($arg);
			}
		}
	}
	
	protected function RenderCachedJs()
	{
		$args = func_get_args();
		$files = array();
		$cached_files = array();
		
		foreach($args as $arg)
		{
			if (is_string($arg))
			{
				$files[] = $arg;
			}
			elseif (is_array($arg))
			{
				foreach($arg as $item)
				{
					$files[] = $item;
				}
			}
		}
		
		foreach($files as $file)
		{
			if (strpos($file, ".min"))
			{
				$cached_files[] = $file;
			}
			else
			{
				$mtime = filemtime($this->js_dir.$file.".js");

				if ($mtime)
				{
					$cached = $file."-".(md5($mtime));
					if (!file_exists($this->js_dir.$cached.".js"))
					{
						file_put_contents($this->js_dir.$cached.".js", 
							minimize_str(file_get_contents($this->js_dir.$file.".js"))
						);
					}

					$cached_files[] = $cached;
				}
			}
		}
		
		$this->RenderJsFiles($cached_files);
	}
	
	protected function RenderJsFilesContent($scripts_names)
	{
		if (is_array($scripts_names))
		{
			foreach($scripts_names as $filename)
			{
				$this->RenderJsFileContent($filename);
			}
		}
		elseif(is_string($scripts_names))
		{
			$this->RenderJsFileContent($scripts_names);
		}
	}
	
	private function RenderJsFileContent($filename)
	{
		if (file_exists("js/private/{$filename}.js"))
		{
			ln(  file_get_contents("js/private/{$filename}.js") );
		}
		else
		{
			El::text("View::_RenderJsFileContent : не найден файл {$filename}");
		}
	}

	protected function RenderJsContent(){}
	
	protected function RenderFavIcon($filename = "favicon.ico")
	{
		echo "<link rel=\"icon\" href=\"http://www.".HOSTNAME."/".$filename."\" type=\"image/x-icon\" />\n";
	}
	
	private function RenderJsBody()
	{
		ln("<script type=\"text/javascript\" language=\"javascript\">");
		ln("<!--");
		
		$this->RenderJsContent();
		
		ln("//-->");
		ln("</script>");
	}
}

?>
